// 权限控制
import router from './router'
import { getToken } from './utils/auth'

const WHITE_LIST = ['/login', '/404']
router.beforeEach((to, from, next) => {
  const token = getToken()
  if (token) {
    // 已登录时禁止访问登录页
    if (to.path === '/login') {
      next('/workbench') // 确保路径与登录后跳转路径一致
    } else {
      next()
    }
  } else {
    // 未登录时放行白名单，否则跳转登录页
    if (WHITE_LIST.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})
